package com.cn.qgx428.lazydiary.Utils

import android.content.Context
import android.content.SharedPreferences
import com.cn.qgx428.lazydiary.APP

/**
 * Created by QinX on 2017/12/14.
 */
class SPUtil private constructor(){

    companion object {
        /**
         * 检查用户是否为安装后第一次打开
         */
        public fun isFirsOpen(): Boolean{
            val sp = APP.getAppContext().getSharedPreferences("user", Context.MODE_PRIVATE)
            val isFirst = sp.getString("firstOpen","")

            return !isFirst.equals("1")
        }

        /**
         * 用户已使用此App
         */
        public fun userInstalledApp(){
            val sp = APP.getAppContext().getSharedPreferences("user", Context.MODE_PRIVATE)
            sp.edit().putString("firstOpen","1").commit()
        }

        public fun currntUserId(): String {
            return get().getString("id","")
        }

        public fun saveUserInfo(phone: String, pwd: String, id: String, nickName: String){
            val sp = APP.getAppContext().getSharedPreferences("user", Context.MODE_PRIVATE)
            sp.edit().putString("phone",phone)
                    .putString("pwd", pwd)
                    .putString("id",id)
                    .putString("nickName",nickName)
                    .commit()
        }

        public fun get(spName : String = "user") : SharedPreferences {
           return APP.getAppContext().getSharedPreferences(spName, Context.MODE_PRIVATE)
        }

        public fun rememberUserLogin(){
            get().edit().putString("isLogin","yes").commit()
        }

        public fun userLogOut(){
            get().edit().putString("isLogin","no").commit()
        }

        public fun checkUserLogin(): Boolean{
            return "yes".equals(get().getString("isLogin","no"))
        }

        /**
         * 存储用户账单预算
         */
        fun saveBillBudget(budget: String){
            val sp = APP.getAppContext().getSharedPreferences("user", Context.MODE_PRIVATE)
            sp.edit().putString("budget", budget)
                    .commit()
        }

        /**
         * 获取用户账单预算
         */
        fun getBillBudget() =
                APP.getAppContext().getSharedPreferences("user", Context.MODE_PRIVATE)
                        .getString("budget", "0")
    }
}